﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Model;
using Shop.Model.Custom;

namespace Shop.Framework.Services {
    public class CartService:BaseService {

        public OrderItem CreateOrder(Product product) {
            return CreateOrder(product, 1);
        }

        public OrderItem CreateOrder(Product product, int quantity) {
            OrderItem item = new OrderItem {
                IdProduct = product.IdProduct,
                Name = product.Name,
                Product = product,
                Quantity = quantity,
                TaxedPrice = product.TaxedPrice
            };

            return item;
        }


        public bool FinishPayment(CustomerOrder order) {

            try {
                ShopDatabaseDataContext context = Context;
                order.OrderItems.AddRange(WebCart.Self.OrderItem);
                context.CustomerOrders.InsertOnSubmit(order);
                context.SubmitChanges();
                WebCart.Self.OrderItem = order.OrderItems.ToList();
                WebCart.Self.CustomerOrder = order;
                
            } catch (Exception ex) {
                throw new Exception("Payment did not succeed..", ex);
            }

            return true;
        }
    }
}
